package com.ethan.cj.ssm.controller;

import com.ethan.cj.ssm.entity.Dept;
import com.ethan.cj.ssm.entity.Mechanism;
import com.ethan.cj.ssm.entity.People;
import com.ethan.cj.ssm.entity.Role;
import com.ethan.cj.ssm.service.DeptService;
import com.ethan.cj.ssm.service.MechanismService;
import com.ethan.cj.ssm.service.PeopleService;
import com.ethan.cj.ssm.service.RoleService;
import com.ethan.cj.ssm.util.JsonUtil;
import com.ethan.cj.ssm.util.PageBean;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人员管理控制层
 */
@Controller
@Log4j
@RequestMapping("people")
public class PeopleController {
    @Autowired
    private PeopleService peopleService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private MechanismService mechanismService;
    @Autowired
    private DeptService deptService;

    /**
     * 跳转分页查询视图
     * @return 查询视图
     */
    @RequestMapping("/list")
    public String toPagePeopleList(){
        return "zyp/PagePeopleList";
    }

    /**
     * 查询视图
     * @param page
     * @param limit
     * @param param
     * @return
     */
    @RequestMapping("/pagePeople")
    @ResponseBody
    public Map<Object, Object> pagePeopleList(@RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam(value = "param")  String  param){
        PageBean pageBean=new PageBean(page, limit);

        Map map = JsonUtil.parseObject(param, Map.class);
        return peopleService.pagePeopleList(pageBean,map);
    }

    @RequestMapping("/add")
    public String toAddPeople(Model model){
        List<Role> roles=roleService.list();
        List<Dept> depts=deptService.allselect();
        List<Mechanism> mechanisms=mechanismService.listMechanism();
        model.addAttribute("roles",roles);
        model.addAttribute("depts",depts);
        model.addAttribute("mechanisms",mechanisms);
        return "zyp/AddPeople";
    }

    @RequestMapping("/insertPeople")
    @ResponseBody
    public Map<Object, Object> insertPeople(@RequestParam("people") String people){

        System.out.println(people);

        Map<Object, Object> result= peopleService.insertPeople(people);
        return result;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/deletePeople")
    @ResponseBody
    public Map<Object, Object> deletePeople(@RequestParam("id") int id){

        Map<Object, Object> result= peopleService.deletePeppleById(id);
        return result;
    }

    /**
     * 通过用户名查找
     * @param pname
     * @return
     */
    @RequestMapping("/findPname")
    @ResponseBody
    public Map<Object, Object> findPname(@RequestParam("pname")String pname){
        People people=peopleService.selectPeopleByName(pname);
        Map<Object, Object> rut=new HashMap<>();
        if(people!=null){
            rut.put("msgs",0);
        }else {
            rut.put("msgs",1);
        }
        return rut;
    }

    /**
     * 通过用户名查找不是自己
     * @param pname
     * @return
     */
    @RequestMapping("/findPn")
    @ResponseBody
    public Map<Object, Object> findPn(@RequestParam("pname")String pname,@RequestParam("pid")int pid){
        People people=peopleService.selectPeopleName(pname, pid);
        Map<Object, Object> rut=new HashMap<>();
        if(people!=null){
            rut.put("msgs",0);
        }else {
            rut.put("msgs",1);
        }
        return rut;
    }

    /**
     * 通过手机号查找
     * @param phone
     * @return
     */
    @RequestMapping("/findPhone")
    @ResponseBody
    public Map<Object, Object> findPhone(@RequestParam("phone")String phone){
        People people=peopleService.selectPeopleByPhonenum(phone);
        Map<Object, Object> rut=new HashMap<>();
        if(people!=null){
            rut.put("msgs",0);
        }else {
            rut.put("msgs",1);
        }
        return rut;
    }

    /**
     * 通过手机号查找
     * @param phone
     * @return
     */
    @RequestMapping("/findP")
    @ResponseBody
    public Map<Object, Object> findP(@RequestParam("phone")String phone,@RequestParam("pid")int pid){
        People people=peopleService.selectPeoplePhoneNum(phone, pid);
        Map<Object, Object> rut=new HashMap<>();
        if(people!=null){
            rut.put("msgs",0);
        }else {
            rut.put("msgs",1);
        }
        return rut;
    }
    /**
     * 跳转修改页面
     * @param pid
     * @param model
     * @return
     */
    @RequestMapping("/Update")
    public String updatePeople(@RequestParam("pid")int pid,Model model){
        People people=peopleService.selectPeopleByPid(pid);
        model.addAttribute("people",people);
        List<Role> roles=roleService.list();
        List<Dept> depts=deptService.allselect();
        List<Mechanism> mechanisms=mechanismService.listMechanism();
        model.addAttribute("roles",roles);
        model.addAttribute("depts",depts);
        model.addAttribute("mechanisms",mechanisms);
        return "zyp/UpdatePeople";
    }

    /**
     * 修改
     * @param people
     * @return
     */
    @RequestMapping("/updatePeople")
    @ResponseBody
    public Map<Object, Object> updatePeople(@RequestParam("people") String people){

        System.out.println(people);

        Map<Object, Object> result= peopleService.updatePeople(people);
        return result;
    }

    /**
     * 修改状态
     * @param pid
     * @param status
     * @return
     */
    @RequestMapping("/updateStatus")
    @ResponseBody
    public Map<Object, Object> updateStatus(@RequestParam("pid") int pid,@RequestParam("status") int status){
        Map<Object, Object> result= peopleService.updateStatus(pid, status);
        return result;
    }



}
